﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Gift.WebApp;
using Gift.Model;

namespace WebSite.admin.company
{
    public partial class companyRule : BaseAdmin
    {

        public string ParamCompanyId
        {
            get
            {
                return Request.QueryString["id"];
            }
        }
    

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.CompanyList);
                this.BuildRuleList();
            }
        }

        private void BuildRuleList()
        {
            StringBuilder strHead = new StringBuilder();
            StringBuilder strBody = new StringBuilder();
            StringBuilder strTable = new StringBuilder();
            StringBuilder strCheckedAdmin = new StringBuilder(",");
            DataSet ds = new DataSet();
            DataSet dsSub = new DataSet();
            string sCompanyID = Request.QueryString["id"];
            ds = BaseFactory.theRule.GetListByLevel(0, int.Parse(base.Administrator.CompanyId), true);

            dsSub = BaseFactory.theRule.GetListByLevel(1, int.Parse(base.Administrator.CompanyId), true);
        
            strTable.Append("<table class='tableRule' border=\"1\" cellpadding=\"3\" cellspacing=\"1\" bordercolor=\"#E2E7EB\">");
            if (ds.Tables.Count > 0)
            {
                int iRowIndex = 0;

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (iRowIndex % 8 == 0)
                    {
                        strHead.Append("<tr class='ruleHead'*>");
                        strBody.Append("<tr>");
                    }

                    strHead.Append("<td width='100px'>" + dr["Description"].ToString() + "</td>");
                    strBody.Append("<td valign='top'>");
                    if (dsSub.Tables.Count > 0)
                    {
                        dsSub.Tables[0].DefaultView.RowFilter = " parentID = " + dr["ruleID"].ToString();

                        for (int i = 0; i < dsSub.Tables[0].DefaultView.Count; i++)
                        {
                            string sChecked = "";
                            if (BaseFactory.theRule.GetRuleByCompayIdAndRuleId(int.Parse(sCompanyID), int.Parse(dsSub.Tables[0].DefaultView[i]["ruleID"].ToString())))
                            {
                                sChecked = "checked='checked'";
                                strCheckedAdmin.Append(dsSub.Tables[0].DefaultView[i]["ruleID"] + ",");
                            }
                            strBody.Append("<br/><input type='checkbox' onclick='setAdminId(this.id)' " + sChecked + " id='" + dsSub.Tables[0].DefaultView[i]["ruleID"].ToString() + "'/>" + dsSub.Tables[0].DefaultView[i]["Description"].ToString());
                        }
                    }
                    strBody.Append("</td>");

                    if (iRowIndex != 0 && iRowIndex % 7 == 0)
                    {
                        strBody.Append("</tr>");
                        strHead.Append("</tr>");
                        strTable.Append(strHead.ToString());
                        strTable.Append(strBody.ToString());
                        strBody = new StringBuilder();
                        strHead = new StringBuilder();
                    }
                    iRowIndex++;
                }

                if (iRowIndex % 8 != 0)
                {
                    if (iRowIndex % 8 == 1)
                    {
                        strBody.Append("<td></td><td></td><td></td><td></td><td></td><td></td><td></td>");
                        strHead.Append("<td></td><td></td><td></td><td></td><td></td><td></td><td></td>");
                    }
                    else if (iRowIndex % 8 == 2)
                    {
                        strBody.Append("<td></td><td></td><td></td><td></td><td></td><td></td>");
                        strHead.Append("<td></td><td></td><td></td><td></td><td></td><td></td>");
                    }
                    else if (iRowIndex % 8 == 3)
                    {
                        strBody.Append("<td></td><td></td><td></td><td></td><td></td>");
                        strHead.Append("<td></td><td></td><td></td><td></td><td></td>");
                    }
                    else if (iRowIndex % 8 == 4)
                    {
                        strBody.Append("<td></td><td></td><td></td><td></td>");
                        strHead.Append("<td></td><td></td><td></td><td></td>");
                    }
                    else if (iRowIndex % 8 == 5)
                    {
                        strBody.Append("<td></td><td></td><td></td>");
                        strHead.Append("<td></td><td></td><td></td>");
                    }
                    else if (iRowIndex % 8 == 6)
                    {
                        strBody.Append("<td></td><td></td>");
                        strHead.Append("<td></td><td></td>");
                    }
                    else if (iRowIndex % 8 == 7)
                    {
                        strBody.Append("<td></td>");
                        strHead.Append("<td></td>");
                    }
                    strBody.Append("</tr>");
                    strHead.Append("</tr>");
                    strTable.Append(strHead.ToString());
                    strTable.Append(strBody.ToString());
                }

            }

            strTable.Append("</table>");
            this.ltRuleList.Text = strTable.ToString();
            this.hdList.Value = strCheckedAdmin.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string sRuleIds = string.Empty;
            if (this.hdList.Value.Length > 1)
            {
                sRuleIds = this.hdList.Value.Substring(1, this.hdList.Value.Length - 2);
            }
            else
            {
                sRuleIds = "0,0";
            }

            BaseFactory.theCompanyRule.SaveCompanyRule(int.Parse(ParamCompanyId), sRuleIds);
            Response.Write("<script>alert('保存成功！');window.location.href='CompanyUserList.aspx?id=" + ParamCompanyId + "';</script>");
        }
    }
}